package com.sfx.concurrent.demo.concurrent.FixedRunOrder;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-09
 * Time: 19:19
 */
@Slf4j(topic = "c.AwaitSignal")
public class AwaitSignal {
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition condition = lock.newCondition();
    private static boolean t2Running = false;
    public static void main(String[] args) {
        new Thread(()->{
            lock.lock();
            try{
                while(!t2Running) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("1");
            }finally {
                lock.unlock();
            }
        },"t1").start();
        new Thread(()->{
            lock.lock();
            try{
                log.debug("2");
                t2Running = true;
                condition.signal();
            }finally {
                lock.unlock();
            }
        },"t2").start();
    }
}